﻿#include <iostream>
#include<curl/curl.h>
#include<string>
#include<map>
#include<vector>
#include<regex>
#include<filesystem>
using namespace std;
const static string URL = "http://127.0.0.1:9999";
//设置一个图片得到保存的路径
const static string PATH = "./image";
size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* output) 
{
    size_t totalSize = size * nmemb;
    output->append((char*)contents, totalSize);
    return totalSize;
}
//编写一个发起请求的函数，接受返回数据
//User-Agent  headers{"Content-type", "User-Agent":,"cookies":} =>map
string request_get(const string& ur1, map<string, string>headers = map<string, string>())
{
    CURL* curl;
    CURLcode statusCode;
    string htmlData;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl)
    {
        // 设置要抓取的⽹⻚地址
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        //判断是否有cookies,如果有就设置请求头中的cookie
        if (headers.find("cookies") != headers.end())
        {
            curl_easy_setopt(curl, CURLOPT_COOKIE, headers["cookies"].c_str());
        }
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
        // 设置回调函数，⽤于处理获取到的响应数据
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlData);
        // 执⾏ HTTP 请求
        auto res = curl_easy_perform(curl);
        if (res != CURLE_OK)
        {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        }
        //输出获取到的网页内容
        //清理 CURL 实例
        curl_easy_cleanup(curl);
        return htmlData;
    }
    else
    {
    std:cerr << "curl_easy_init()failed." << std::endl;
        return "";
    }
    curl_global_cleanup();
}

//判断value 是否在vt中
bool contains(vector<string>& vt, const string& value)
{
    return find(vt.begin(), vt.end(), value) != vt.end();
}
vector<map<string,string>> getImageUrl(const string& html)
{
    //如何获取？？？正则表达式获取所有的image的链接  ./img/movie2.jpg  ./img/movie1.jpg

    regex reg("/imag/(\\w+)\\.jpg|png");
    auto end_it = sregex_iterator();
    //vector<string> vt;
    vector<map<string, string>> vt;
    //v0存url
    vector<string> vt0;
    sregex_iterator it(html.begin(), html.end(), reg);
    //如何去除重复的IMAGEURL
    smatch result;
    map<string, string>URLMAP;
    //url=> image_name=>
    for (; it != end_it; it++)
    {
        if (contains(vt0, it->str()))
        {
            continue;
        }
        vt0.push_back(it->str());
        result = *it;
        URLMAP["url"] = result[0].str();
        URLMAP["image_name"] = result[1].str() + "." + result[2].str();
        vt.push_back(URLMAP);
    }
    return vt;
    //如何获取???正则表达式获取所有的imag的链接   ./img/mobie2.jpg  ./img/mobie1.jpg
}
int main()
{
    SetConsoleOutputCP(65001);
    if (!filesystem:exists(PATH))
    {
        filesystem::create_directory(PATH);
    }
    map<string, string>headders;
    headers["cookies"] = "PHPSESSID=9lqt15n0tr35danl5708idk8ar";
    string html;
    html = request_get("http://127.0.0.1:999", headers);
    //cout<<html<<endl;
    vector<map<string, string>> vt = getImageUrl(html);
    for (auto w : vt)
    {
        for (auto u : w)
        {
            cout << u.first << " " << u.second << endl;
        }
    }
    map<string, string>urlmap;
    string url;
    string image_name;
    while (!vt.empty())
    {
        urlmap = vt.back();
        vt.pop_back();
        url = URL + "/" + urlmap["url"];
        image_name = PATH + "/" + urlmap["image_name"];
        const char* file_url = url.c_str();
        const char* file_name = image_name.c_str();
        download_file(file_url, file_name);
    }
   
    
}

